<template>
	<view class="user-list">
		<nav-bar lefticon="iconfont icon-2fanhui" middlebar="地址列表" >
			<template slot="right-click">
				<view class="iconfont icon-jia mr-2" @click="addlist"></view>
			</template>
		</nav-bar>
		
		<block v-for="(item,index) in list" :key="index">
			<uni-swipe-action>
				<uni-swipe-action-item :right-options="options"  @click="bindClick($event,index)">
					<uni-list-item showArrow @click="choose(item)" clickable>
						<template slot="header">
							<view class="text-secondary">
								<view class="d-flex a-center">
									<text class="main-text-color">{{item.name}}</text>
									{{item.phone}}
									<text class="main-text-color" v-if="item.default===1">[默认]</text>
								</view>
								<view>
									{{item.province}} {{item.city}} {{item.district}}
								</view>
								<view>
									{{item.address}}
								</view>
							</view>
						</template>
					</uni-list-item>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</block>
	</view>
</template>

<script>
	import navBar from "@/components/common/nav-bar.vue"
	import uniSwipeAction from "@/components/uni-swipe-action/uni-swipe-action.vue"
	import uniSwipeActionItem from "@/components/uni-swipe-action-item/uni-swipe-action-item.vue"
	import {mapState,mapGetters,mapMutations} from "vuex"
	export default {
		data() {
			return {
				user_id:'',
				isChoose:false,
				options: [{
					text: '修改',
					style: {
						backgroundColor: '#007aff'
					}
				}, {
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
			}
		},
		onLoad(e) {
			if (e.type === 'choose') {
				this.isChoose = true
			}
			// this.user_id = e.userID
			this.getuseraddress()
		},
		components:{navBar,uniSwipeAction,uniSwipeActionItem},
		computed:{
			...mapState({
				list:state=>state.path.list,
				tokenx:state=>state.login.tokenx
			}),
			...mapGetters([
				'defaultPath'
			])
		},
		methods: {
			bindClick(value,i){
				switch(value.index){
					case 0:  //修改
					let obj = {
						index:i,
						item:this.list[i]
					}
					// 加上是否默认
					obj.item.default = (i === 0) ? 1 : 0;
					setTimeout(()=> {
						uni.navigateTo({
							url: '../user-path-edit/user-path-edit?data='+JSON.stringify(obj),
						});
					},50);
					break;
					case 1:  //删除
					uni.showModal({
						content: '要删除该收货地址吗？',
						success: (res)=> {
							if (res.confirm) {
								this.$H.post('/useraddress/del',{
									user_id:this.list[i].user_id,
									id:this.list[i].id
								},{
									token:true
								}).then(res=>{
									this.delPath(i)
									uni.showToast({
										title: '删除成功'
									});
								})
							} 
						}
					});
					break;
				}
			},
			getuseraddress(){
				this.$H.post('/useraddress/list',{
					user_id:this.tokenx
				},{
					token:true
				}).then(res=>{
					this.setpathlist(res.msg)
				})
			},
			addlist(){
				uni.navigateTo({
					url:'../user-path-edit/user-path-edit?userID='+this.list[0].user_id
				})
			},
			choose(item){
				if(this.isChoose){
					// 通知订单提交页修改收货地址
					uni.$emit('choosePath',item)
					// 关闭当前页面
					uni.navigateBack({
						delta: 1
					});
				}
			},
			...mapMutations([
				'setpathlist',
				'setuserid',
				'delPath'
			])
		}
	}
</script>

<style>

</style>
